package com.cn.easy.export

import com.cn.easy.export.model.excel.*
import com.cn.easy.export.service.excel.ExcelExportService
import org.apache.poi.ss.usermodel.BorderStyle
import org.apache.poi.ss.usermodel.HorizontalAlignment
import org.apache.poi.ss.usermodel.VerticalAlignment
import java.io.FileOutputStream
import kotlin.test.Test

class Test1 {
    @Test
    fun test1(): Unit {
        val students = arrayListOf(
            Student(1, "u1", "一班", 20),
            Student(2, "u2", "一班", 22),
            Student(3, "u3", "一班", 33),
        )
        val headers = arrayListOf<ExcelColumn<Student>>(
            SingleExcelColumn(ExcelTitle("ID")) { it.id },
            SingleRowMultiExcelColumn(
                title = ExcelTitle("基本信息"),
                children = arrayListOf(
                    SingleExcelColumn(ExcelTitle("姓名")) { it.name },
                    SingleExcelColumn(ExcelTitle("年龄")) { it.age },
                    SingleExcelColumn(ExcelTitle("班级")) { it.`class` },
                )
            )
        )

        val service = ExcelExportService(
            ExportParam(
                columns = headers,
                list = students,
            )
        )
        service.export()
            .use {
                it.write(FileOutputStream("D:\\download\\header1.xlsx"))
            }
    }

    @Test
    fun test2() {
        val students = arrayListOf(
            Student(1, "u1", "一班", 20),
            Student(2, "u2", "一班", 22),
            Student(3, "u3", "一班", 33),
        )
        val headers = arrayListOf<ExcelColumn<Student>>(
            SingleExcelColumn(ExcelTitle("ID")) { it.id },
            SingleRowMultiExcelColumn(
                title = ExcelTitle("基本信息"),
                children = arrayListOf(
                    SingleExcelColumn(ExcelTitle("姓名")) { it.name },
                    SingleExcelColumn(ExcelTitle("年龄")) { it.age },
                    SingleExcelColumn(ExcelTitle("班级")) { it.`class` },
                ),
                cellStyle = { workbook, row ->
                    val cellStyle = workbook.createCellStyle()
                    cellStyle.alignment = HorizontalAlignment.CENTER
                    cellStyle.borderTop = BorderStyle.THIN
                    cellStyle.borderLeft = BorderStyle.THIN
                    cellStyle.borderRight = BorderStyle.THIN
                    cellStyle.borderBottom = BorderStyle.THIN
                    val font = workbook.createFont()
                    font.bold = true
                    font.fontName = "微软雅黑"
                    font.fontHeightInPoints = 12
                    cellStyle.setFont(font)

                    cellStyle
                }
            ),
            SingleRowMultiExcelColumn(
                title = ExcelTitle("基本信息"),
                children = arrayListOf(
                    SingleRowMultiExcelColumn(
                        title = ExcelTitle("姓名"),
                        children = arrayListOf(
                            SingleRowMultiExcelColumn(
                                title = ExcelTitle("姓"),
                                children = arrayListOf(
                                    SingleExcelColumn(ExcelTitle("1")) { it.name },
                                    SingleExcelColumn(ExcelTitle("2")) { it.name },
                                ),
                            ),
                            SingleExcelColumn(ExcelTitle("名")) { it.name },
                        )
                    ),
                    SingleRowMultiExcelColumn(
                        title = ExcelTitle("年龄"),
                        children = arrayListOf(
                            SingleExcelColumn(ExcelTitle("1")) { it.age },
                            SingleExcelColumn(ExcelTitle("2")) { it.age },
                        )

                    ),
                    SingleExcelColumn(ExcelTitle("班级")) { it.`class` },
                )
            ),
        )

        val service = ExcelExportService(
            ExportParam(
                sheetName = "aaa",
                excelTitle = ExcelTitle(
                    "学生信息"
                ) { workbook ->
                    val cellStyle = workbook.createCellStyle()
                    //下边框
                    cellStyle.borderBottom = BorderStyle.THIN
                    //左边框
                    cellStyle.borderLeft = BorderStyle.THIN
                    //上边框
                    cellStyle.borderTop = BorderStyle.THIN
                    //右边框
                    cellStyle.borderRight = BorderStyle.THIN
                    cellStyle.alignment = HorizontalAlignment.CENTER

                    val font = workbook.createFont()
                    font.bold = true
                    font.fontName = "微软雅黑"
                    font.fontHeightInPoints = 24
                    cellStyle.setFont(font)

                    cellStyle
                },
                columns = headers,
                list = students,
                headerStyle = { workbook ->
                    val cellStyle = workbook.createCellStyle()
                    cellStyle.alignment = HorizontalAlignment.CENTER
                    cellStyle.verticalAlignment = VerticalAlignment.CENTER
                    val font = workbook.createFont()
                    font.bold = true
                    font.fontName = "微软雅黑"
                    font.fontHeightInPoints = 12
                    cellStyle.setFont(font)

                    cellStyle
                }
            ),
            ExportParam(
                sheetName = "bbb",
                excelTitle = ExcelTitle(
                    "学生信息"
                ) { workbook ->
                    val cellStyle = workbook.createCellStyle()
                    //下边框
                    cellStyle.borderBottom = BorderStyle.THIN
                    //左边框
                    cellStyle.borderLeft = BorderStyle.THIN
                    //上边框
                    cellStyle.borderTop = BorderStyle.THIN
                    //右边框
                    cellStyle.borderRight = BorderStyle.THIN
                    cellStyle.alignment = HorizontalAlignment.CENTER

                    val font = workbook.createFont()
                    font.bold = true
                    font.fontName = "微软雅黑"
                    font.fontHeightInPoints = 24
                    cellStyle.setFont(font)

                    cellStyle
                },
                columns = headers,
                list = students,
                headerStyle = { workbook ->
                    val cellStyle = workbook.createCellStyle()
                    cellStyle.alignment = HorizontalAlignment.CENTER
                    cellStyle.verticalAlignment = VerticalAlignment.CENTER
                    val font = workbook.createFont()
                    font.bold = true
                    font.fontName = "微软雅黑"
                    font.fontHeightInPoints = 12
                    cellStyle.setFont(font)

                    cellStyle
                }
            )
        )
        service.export()
            .use {
                it.write(FileOutputStream("D:\\download\\header2.xlsx"))
            }
    }

    private data class Student(
        val id: Int,
        val name: String,
        val `class`: String,
        val age: Int,
    )
}